﻿namespace KeRui.DAL
{
    using KeRui.DBUtility;
    using KeRui.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;

    public class AdminFlag
    {
        public int Add(KeRui.Model.AdminFlag model)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.Int, 4), new SqlParameter("@ParentID", SqlDbType.Int, 4), new SqlParameter("@IsNav", SqlDbType.Int, 4), new SqlParameter("@HaveChildNav", SqlDbType.Bit, 1), new SqlParameter("@Flag", SqlDbType.VarChar, 50), new SqlParameter("@FlagName", SqlDbType.NVarChar, 50), new SqlParameter("@FlagAction", SqlDbType.VarChar, 50), new SqlParameter("@FlagGroup", SqlDbType.Int, 4), new SqlParameter("@FlagType", SqlDbType.Int, 4), new SqlParameter("@AppUrl", SqlDbType.VarChar, 0xff), new SqlParameter("@OrderID", SqlDbType.Int, 4), new SqlParameter("@Icon", SqlDbType.VarChar, 50), new SqlParameter("@IsOpen", SqlDbType.Bit, 1) };
            parameters[0].Direction = ParameterDirection.Output;
            parameters[1].Value = model.ParentID;
            parameters[2].Value = model.IsNav;
            parameters[3].Value = model.HaveChildNav;
            parameters[4].Value = model.Flag;
            parameters[5].Value = model.FlagName;
            parameters[6].Value = model.FlagAction;
            parameters[7].Value = model.FlagGroup;
            parameters[8].Value = model.FlagType;
            parameters[9].Value = model.AppUrl;
            parameters[10].Value = model.OrderID;
            parameters[11].Value = model.Icon;
            parameters[12].Value = model.IsOpen;
            DbHelperSQL.RunProcedure("AdminFlag_ADD", parameters, out num);
            return (int) parameters[0].Value;
        }

        public int Delete(int ID)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.Int, 4) };
            parameters[0].Value = ID;
            DbHelperSQL.RunProcedure("AdminFlag_Delete", parameters, out num);
            return ID;
        }

        public bool Exists(int ID)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.Int, 4) };
            parameters[0].Value = ID;
            return (DbHelperSQL.RunProcedure("AdminFlag_Exists", parameters, out num) == 1);
        }

        public KeRui.Model.AdminFlag GetModel(int ID)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.Int, 4) };
            parameters[0].Value = ID;
            KeRui.Model.AdminFlag flag = new KeRui.Model.AdminFlag();
            DataSet set = DbHelperSQL.RunProcedure("AdminFlag_GetModel", parameters, "ds");
            if (set.Tables[0].Rows.Count > 0)
            {
                if (set.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    flag.ID = int.Parse(set.Tables[0].Rows[0]["ID"].ToString());
                }
                if (set.Tables[0].Rows[0]["ParentID"].ToString() != "")
                {
                    flag.ParentID = int.Parse(set.Tables[0].Rows[0]["ParentID"].ToString());
                }
                if (set.Tables[0].Rows[0]["IsNav"].ToString() != "")
                {
                    flag.IsNav = int.Parse(set.Tables[0].Rows[0]["IsNav"].ToString());
                }
                if (set.Tables[0].Rows[0]["HaveChildNav"].ToString() != "")
                {
                    if ((set.Tables[0].Rows[0]["HaveChildNav"].ToString() == "1") || (set.Tables[0].Rows[0]["HaveChildNav"].ToString().ToLower() == "true"))
                    {
                        flag.HaveChildNav = true;
                    }
                    else
                    {
                        flag.HaveChildNav = false;
                    }
                }
                flag.Flag = set.Tables[0].Rows[0]["Flag"].ToString();
                flag.FlagName = set.Tables[0].Rows[0]["FlagName"].ToString();
                flag.FlagAction = set.Tables[0].Rows[0]["FlagAction"].ToString();
                if (set.Tables[0].Rows[0]["FlagGroup"].ToString() != "")
                {
                    flag.FlagGroup = int.Parse(set.Tables[0].Rows[0]["FlagGroup"].ToString());
                }
                if (set.Tables[0].Rows[0]["FlagType"].ToString() != "")
                {
                    flag.FlagType = int.Parse(set.Tables[0].Rows[0]["FlagType"].ToString());
                }
                flag.AppUrl = set.Tables[0].Rows[0]["AppUrl"].ToString();
                if (set.Tables[0].Rows[0]["OrderID"].ToString() != "")
                {
                    flag.OrderID = int.Parse(set.Tables[0].Rows[0]["OrderID"].ToString());
                }
                flag.Icon = set.Tables[0].Rows[0]["Icon"].ToString();
                if (set.Tables[0].Rows[0]["IsOpen"].ToString() != "")
                {
                    if ((set.Tables[0].Rows[0]["IsOpen"].ToString() == "1") || (set.Tables[0].Rows[0]["IsOpen"].ToString().ToLower() == "true"))
                    {
                        flag.IsOpen = true;
                    }
                    else
                    {
                        flag.IsOpen = false;
                    }
                }
                if (set.Tables[0].Rows[0]["CreateTime"].ToString() != "")
                {
                    flag.CreateTime = DateTime.Parse(set.Tables[0].Rows[0]["CreateTime"].ToString());
                }
                return flag;
            }
            return null;
        }

        public int Update(KeRui.Model.AdminFlag model)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.Int, 4), new SqlParameter("@ParentID", SqlDbType.Int, 4), new SqlParameter("@IsNav", SqlDbType.Int, 4), new SqlParameter("@HaveChildNav", SqlDbType.Bit, 1), new SqlParameter("@Flag", SqlDbType.VarChar, 50), new SqlParameter("@FlagName", SqlDbType.NVarChar, 50), new SqlParameter("@FlagAction", SqlDbType.VarChar, 50), new SqlParameter("@FlagGroup", SqlDbType.Int, 4), new SqlParameter("@FlagType", SqlDbType.Int, 4), new SqlParameter("@AppUrl", SqlDbType.VarChar, 0xff), new SqlParameter("@OrderID", SqlDbType.Int, 4), new SqlParameter("@Icon", SqlDbType.VarChar, 50), new SqlParameter("@IsOpen", SqlDbType.Bit, 1) };
            parameters[0].Value = model.ID;
            parameters[1].Value = model.ParentID;
            parameters[2].Value = model.IsNav;
            parameters[3].Value = model.HaveChildNav;
            parameters[4].Value = model.Flag;
            parameters[5].Value = model.FlagName;
            parameters[6].Value = model.FlagAction;
            parameters[7].Value = model.FlagGroup;
            parameters[8].Value = model.FlagType;
            parameters[9].Value = model.AppUrl;
            parameters[10].Value = model.OrderID;
            parameters[11].Value = model.Icon;
            parameters[12].Value = model.IsOpen;
            DbHelperSQL.RunProcedure("AdminFlag_Update", parameters, out num);
            return model.ID;
        }
    }
}

